package planit.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import planit.model.Event;
import planit.model.Model;
import planit.util.ColorTools;
import planit.util.ExtendedDate;


@SuppressWarnings("serial")
public class SchedulePane extends JPanel {

	protected ArrayList<Event> events;
	private JPanel[] panels;
	private double minutesPerPixel = 1;
	protected int days;
	private Date startDate;
	protected int columnsPerDay;
	protected HashMap<Event, ArrayList<Event>> collisions;
	Calendar cal;

	private Date[] lastFilledEndTime;

	protected Model model;

	public SchedulePane() {

	}

	public SchedulePane(Date startDate, int days, Model model) {
		this.model = model;
		setStartDate(startDate);
		this.days = days;
		init();
		addAllEvents();
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public void init() {
		setVisible(true);
		cal = new GregorianCalendar();
	}

	protected void addAllEvents() {
		setEvents(model.getSchedule().getEvents(this.getStartDate(), ExtendedDate.dateWithAddedDays(this.getStartDate(), days)));
		collisions = new HashMap<Event, ArrayList<Event>>();
		columnsPerDay = 1;
		int temp = 1;
		for (Event a : events) {
			collisions.put(a, new ArrayList<Event>());
			for (Event b : events) {
				if (!a.equals(b)) {
					if (a.getStartTime().getTime() >= b.getEndTime().getTime() || a.getEndTime().getTime() <= b.getStartTime().getTime()) {
					} 
					else {
						collisions.get(a).add(b);
						temp++;
					}
				}
			}
			columnsPerDay = Math.max(columnsPerDay, temp);
			temp = 1;
		}
		this.repaint();
	}

	protected int minutesToHeight(long l) {
		minutesPerPixel = 1440/this.getHeight();
		return (int)(l / minutesPerPixel);
	}

	public void setEvents(ArrayList<Event> events2) {
		this.events = events2;
	}
	
	protected void paintComponent(Graphics g) {
		g.setColor(Color.RED);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
	}
    
    protected void drawEvent(Graphics g, Event e, int startX, int startY, int height, int width) {
		g.setColor(Color.BLACK);
		g.fillRect(startX, startY, width, minutesToHeight(e.getDuration()));
		g.setColor(Color.BLUE);
		g.fillRect(startX+2, startY+2, width-4, minutesToHeight(e.getDuration())-4);
		g.setColor(Color.WHITE);
		g.drawString(e.getName(), startX+6, startY+13);
    }
    


	/*
	 * @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated
	 * method stub
	 * 
	 * }
	 * 
	 * @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated
	 * method stub }
	 * 
	 * @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated
	 * method stub
	 * 
	 * }
	 * 
	 * @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated
	 * method stub
	 * 
	 * }
	 * 
	 * @Override public void mouseReleased(MouseEvent e) { // TODO
	 * Auto-generated method stub
	 * 
	 * }
	 * 
	 * @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated
	 * method stub
	 * 
	 * }
	 * 
	 * @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated
	 * method stub
	 * 
	 * }
	 * 
	 * @Override public void componentHidden(ComponentEvent arg0) { // TODO
	 * Auto-generated method stub
	 * 
	 * }
	 * 
	 * @Override public void componentMoved(ComponentEvent arg0) { // TODO
	 * Auto-generated method stub
	 * 
	 * }
	 * 
	 * @Override public void componentResized(ComponentEvent arg0) {
	 * updateSize();
	 * 
	 * }
	 * 
	 * @Override public void componentShown(ComponentEvent arg0) { // TODO
	 * Auto-generated method stub }
	 */
}
